/**
 * MainUI.java
 *
 *
 *
 * @author HongKai Guo
 * @version 1.00 2011/4/5
 */

import java.util.ArrayList;
import java.util.Scanner;


public class MainUI {
//menu option
private final int HorLaneMenu = 1;
private final int VerLaneMenu = 2;
private final int OneSimCycle = 3;
private final int NumSimCycle = 4;
private final int Terminate = 5;

//call other class
ProjModel proj = new ProjModel();
GridView gv = new GridView();
Car car = new Car();

//declare Array List
ArrayList<Car> CarArray = new ArrayList<Car>();

    /**
     * To run the entire program
     */
   public void run(){
       gv.setGrid();
       int choice = 0;
       int horLane = 0;
       int verLane = 0;

       while (choice != 5){
            menu();
            choice = input();


            if(choice <1 || choice>5){
                System.out.println("Error invalid choice\nPlease reenter again!");
            }
            else
            {
                System.out.println("You choose : " + choice);
                switch(choice) {
                    // set Horizontal Lane
                    case HorLaneMenu:{
                        while(horLane < 1 || horLane > 3){
                            System.out.println("We are in set Horizontal Lane\nEnter number of Lanes: ");
                            horLane = input();
                            if(horLane >=1 && horLane <= 3){
                                proj.setHorLane(horLane);
                            }else{
                                System.out.println("Error!");
                            }
                        }
                        horLane = 0;
                        break;
                    }

                    // set Vertical Lane
                    case VerLaneMenu:{
                        while(verLane < 1 || verLane > 4){
                            System.out.println("We are in set Vertical Lane\nEnter number of Lanes: ");
                            verLane = input();
                            if(verLane >= 1 || verLane <= 4){
                                proj.setVerLane(verLane);
                            }else{
                                System.out.println("Error!");
                            }
                        }
                        verLane = 0;
                        break;
                    }

                    // Run One Simulation Cycle
                    case OneSimCycle:{
                        gv.setGrid();                   // reset Grid
                        gv.horLane(proj.getHorLane());  // get Horizontal Lane
                        gv.verLane(proj.getVerLane());  // get Vertical Lane

                        // implement Traffic Light
                        int traffHorRow = gv.setTrafficLightsrow(proj.getMiddleHor(),proj.getHorLane());
                        int traffHorCol = gv.setTrafficLightscol(proj.getMiddleVer(),proj.getVerLane());
                        gv.buildTrafficLights(traffHorRow,traffHorCol,proj.getLightHor());

                        int traffVerRow = gv.setTrafficLightsrow(proj.getMiddleHor(),proj.getHorLane());
                        int traffVerCol = proj.getMiddleVer()-1;
                        gv.buildTrafficLights(traffVerRow,traffVerCol,proj.getLightVer());

                        // implement car
                        for(int i = 0; i < car.GenerateCarRandNum();i++){
                            CarArray.add(new Car());
                        }

                        for(int i = 0;i<CarArray.size();i++){
                            int j=i;
                            j = j%2;
                            if(j==0){
                                CarArray.get(i).setCol(car.GenerateRandCarPosrow(proj.getGridSize()));
                                CarArray.get(i).setRow(car.GenerateRandCarPoscol(proj.getHorLane(),proj.getMiddleHor()));
                            }else if(j==1){
                                CarArray.get(i).setRow(car.GenerateRandCarPosrow(proj.getGridSize()));
                                CarArray.get(i).setCol(car.GenerateRandCarPoscol(proj.getVerLane(),proj.getMiddleVer()));
                            }
                        }

                        for(int i = 0;i<CarArray.size();i++){
                            gv.setCarOnGrid(CarArray.get(i).getRow(),CarArray.get(i).getCol());
                        }


                        gv.showGrid();

                        break;
                    }
                    // Run Multiple Cycle
                    case NumSimCycle:{
                        System.out.println("We are in set and run number of simulation cycles\nEnter how many simulation cycles:");
                        int simCycles = input();
                        if(simCycles >=1 && simCycles <=10){
                            int gs = proj.getGridSize();
                            boolean Lighthor = false;
                            boolean Lightver = true;

                            gv.setGrid();
                            gv.horLane(proj.getHorLane());
                            gv.verLane(proj.getVerLane());

                            // implement Traffic Light
                            int traffHorRow = gv.setTrafficLightsrow(proj.getMiddleHor(),proj.getHorLane());
                            int traffHorCol = gv.setTrafficLightscol(proj.getMiddleVer(),proj.getVerLane());
                            gv.buildTrafficLights(traffHorRow,traffHorCol,proj.getLightHor());

                            int traffVerRow = gv.setTrafficLightsrow(proj.getMiddleHor(),proj.getHorLane());
                            int traffVerCol = proj.getMiddleVer()-1;
                            gv.buildTrafficLights(traffVerRow,traffVerCol,proj.getLightVer());

                            // implement car
                            for(int i = 0; i < car.GenerateCarRandNum();i++){
                                CarArray.add(new Car());
                            }

                            for(int i = 0;i<CarArray.size();i++){
                                int j=i;
                                j = j%2;
                                if(j==0){
                                    CarArray.get(i).setCol(car.GenerateRandCarPosrow(proj.getGridSize()));
                                    CarArray.get(i).setRow(car.GenerateRandCarPoscol(proj.getHorLane(),proj.getMiddleHor()));
                                }else if(j==1){
                                    CarArray.get(i).setRow(car.GenerateRandCarPosrow(proj.getGridSize()));
                                    CarArray.get(i).setCol(car.GenerateRandCarPoscol(proj.getVerLane(),proj.getMiddleVer()));
                                }
                            }

                            for(int i = 0;i<CarArray.size();i++){
                                gv.setCarOnGrid(CarArray.get(i).getRow(),CarArray.get(i).getCol());
                            }

                            for(int i = 0;i<simCycles;i++){
                                if(i%3==0){
                                    if(Lighthor == true){
                                        Lighthor = false;
                                        Lightver = true;
                                    }else{
                                        Lighthor = true;
                                        Lightver = false;
                                    }
                                    proj.setLightHor(Lighthor);
                                    proj.setLightVer(Lightver);
                                    gv.buildTrafficLights(traffHorRow,traffHorCol,proj.getLightHor());
                                    gv.buildTrafficLights(traffVerRow,traffVerCol,proj.getLightVer());
                                }

                                // Car move
                                for(int j = 0;j<CarArray.size();j++){
                                    int k = j;
                                    k = k%2;
                                    if(k==0||j==0){
                                        int row = CarArray.get(j).getRow();
                                        int col = CarArray.get(j).getCol();
                                        if(Lighthor == false && col == proj.getMiddleVer()-1){
                                            gv.setCarOnGrid(CarArray.get(j).getRow(),CarArray.get(j).getCol());
                                        }else{

                                            gv.removeCarOnGrid(row,col);
                                            col++;

                                            if(col == gs){
                                                col--;
                                                gv.removeCarOnGrid(row,col);
                                            }else{
                                                CarArray.get(j).setCol(col);
                                                gv.setCarOnGrid(CarArray.get(j).getRow(),CarArray.get(j).getCol());
                                            }
                                        }

                                    }else if(k==1){
                                        int row = CarArray.get(j).getRow();
                                        int col = CarArray.get(j).getCol();
                                        if(Lightver == false && row == proj.getMiddleHor()-1){
                                            gv.setCarOnGrid(CarArray.get(j).getRow(),CarArray.get(j).getCol());
                                        }else{

                                            gv.removeCarOnGrid(row,col);
                                            row++;

                                            if(row == gs){
                                                row--;
                                                gv.removeCarOnGrid(row,col);
                                            }else{
                                                CarArray.get(j).setRow(row);
                                                gv.setCarOnGrid(CarArray.get(j).getRow(),CarArray.get(j).getCol());
                                            }
                                        }
                                    }

                                }

                                gv.showGrid();                                      //show Grid
                                System.out.println("Simulation" + (i+1 + "\n"));
                            }
                        }else{
                            System.out.println("Invalid number Please reenter");
                        }
                        break;
                    }

                    case Terminate:{
                        System.out.println("Thanks for using the program.");
                        break;
                    }
                }
            }
       }
   }

   //menu

    /**
     * to show menu
     */
   public void menu(){
        System.out.println (
        "\n=== MENU ===\n" +
        "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n" +
        "2. Set Number of vertical lanes  (V-street) [min 1, max 4]\n" +
        "3. Run one simulation cycle\n" +
        "4. Set and run number of simulation cycles [min 1, max 10]\n" +
        "5. Terminate\n" +
        "Enter your choice> "
        );
   }

   //input

    /**
     *
     * @return choice choice of the user
     */
   public int input(){
   Scanner input = new Scanner(System.in);
   int choice = input.nextInt();
   return choice;
   }


}